Lists Patterns¶

🎨 Arithmetic Operators¶

In [1]:
7 + 2
Out[1]:
9
In [2]:
7 - 2
Out[2]:
5
In [3]:
7 * 2
Out[3]:
14
In [4]:
7 ** 2
Out[4]:
49

7 ** 2 is $ 7 ^ 2 $

In [5]:
7 / 2
Out[5]:
3.5
In [6]:
7 // 2
Out[6]:
3
In [11]:
7 % 2
Out[11]:
1

$\frac{7}{2} = 3r1$

7 // 2 gives us the $3$

7 % 2 gives us the $1$ (the remainder)

🎨 Comparisons¶

In [12]:
2 > 1
Out[12]:
True
In [13]:
2 > 2
Out[13]:
False
In [16]:
2 >= 2
Out[16]:
True
In [17]:
4 == 4
Out[17]:
True
In [18]:
4 != 6
Out[18]:
True

🖌 Transforming a List¶

transform_a_list.py¶

This demonstrates the mapping pattern.

Map is a mathematical term that means going from one value to a corresponding value.

This is also referred to as the transform pattern.

In [ ]:
%%file for_class/transform_a_list.py
def make_bigger(numbers):
    bigger_numbers = []
    for number in numbers:
        bigger = number * 2
        bigger_numbers.append(bigger)
    return bigger_numbers


if __name__ == '__main__':
    a_few_ints = [1, 2, 3, 4, 5, 6, 7, 8]
    bigger = make_bigger(a_few_ints)
    print(a_few_ints)
    print(bigger)

👨🏼‍🎨 smaller_numbers.py¶

Write a function that creates a new list where each item has been divided by 2.

You can pass lists to functions and return lists from functions.

🖌 Filtering Lists¶

only_odds.py¶

This demonstrates the filter pattern.

A new collection is created with certain items filtered out.

NOTES

  • Step through with debugger
  • Show how numbers are added only if they are odd

🖌 Selecting from a List¶

find_min.py¶

This demonstrates the selection pattern.

A single item is selected from a collection.

NOTES

  • is None: this is how you check if a variable points to None
  • Step through in debugger
  • Look at logic of if condition
    • See how the value of smallest updates only sometimes

🖌 Accumulation¶

average.py¶

This demonstrates the accumulator pattern.

total accumulates the values in the collection.

NOTES

  • Step through in debugger
  • Show how total updates

👩🏻‍🎨 All Together¶

Given a list of numbers, write a function that substracts 7 and removes the negative numbers or numbers greater than 10.

all_together.py¶

Key Ideas¶

  • Arithmetic operators
  • mapping pattern
  • filter pattern
  • selection pattern
  • accumulator pattern